//index.js
//获取应用实例
import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify';

const app = getApp()

Page({
  data: {
    phoneNumber:"",
    password:"",
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  login:function(){
    let that = this;
    wx.request({
      url:"http://8.129.104.110/user/login",
      method:'POST',
      data:{
        password:that.data.password,
        phoneNumber:that.data.phoneNumber
      },
      header:{
       "Content-Type":"application/json"
      },
      success:function(res){
      console.log(res.data)
      if(res.data.code == "2000"){
        Notify({ type: 'success', message: res.data.message });
        app.globalData.id=res.data.data.id;
        app.globalData.roleId=res.data.data.roleId;
        console.log(app.globalData)
        wx.switchTab({
          url: '/pages/home/home',
        })
      }else{
        Notify({ type: 'warning', message: res.data.message });
      }
      },
      fail:function(err){
      console.log(err)
      }
      })
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },

})
